var express = require('express');
var session = require('express-session');

var router = express.Router();
var User = require('../models/user');
var Userinformation = require('../models/userinformation');


/* GET home page. */
router.get('/home', function(req, res, next) {
  res.render('home');

});

/* GET post page. */
router.get('/post', function(req, res, next) {
    res.render('post');

});

/* GET mine page. */
router.get('/mine', function(req, res, next) {
    res.render('mine');

});



/* GET login page*/
router.get('/login',function(req,res,next) {
    res.render('login',{ title: '登录' });
});

/* GET register page*/
router.get('/reg',function(req,res,next) {
    res.render('reg',{ title: '注册' });
});


/* POST login page*/
router.post('/login',function(req,res,next) {
    var username = req.body.username;
    var password = req.body.password;
    if(!username||!password){
        var dataError = {
            status: '0',
            msg: '用户名或密码为空'
        };
        res.end(JSON.stringify(dataError));
    }


    var dataSuccess = {
        status: '200',
        msg: '登录成功',
        data: {
            username: username
        }
    };
    var dataError = {
        status: '99',
        msg: '用户名或密码错误'
    };

    // 打印post请求的数据内容
    console.log(req.body);
    console.log(req.body.username);
    console.log(req.body.password);

    var newUser = new User({
        username:req.body.username,
        password:req.body.password
    });

    if (req.session.user&&req.session.user.username==newUser.username&&req.session.user.password==newUser.password) {
        var dataError = {
            status: '201',
            msg: '用户名已登录'
        };
        res.end(JSON.stringify(dataError));

    }


    User.findOne({username:newUser.username},function (err,user) {
        req.session.user = null;
        if(!user){
            var dataError = {
                status: '0',
                msg: '用户名不存在'
            };
            res.end(JSON.stringify(dataError));
        }
        else if(user.password!=req.body.password){
            var dataError = {
                status: '0',
                msg: '用户名密码错误'
            };
            res.end(JSON.stringify(dataError));
        }
        else if (err){
            var dataError = {
                status: '0',
                msg: err.message
            };
            res.end(JSON.stringify(dataError));
        }
        else {
            req.session.user = newUser;
            res.end(JSON.stringify(dataSuccess));

        }

    })



});


/* POST reg page*/
router.post('/reg',function(req,res,next) {

    var username = req.body.username;
    var password = req.body.password;
    if(!username||!password){
        var dataError = {
            status: '0',
            msg: '用户名或密码为空'
        };
        res.end(JSON.stringify(dataError));
    }

    var dataSuccess = {
        status: '200',
        msg: '注册成功',
        data: {
            username: username
        }
    };
    var dataError = {
        status: '99',
        msg: '用户名或密码错误'
    };

    // 打印post请求的数据内容
    console.log(req.body);
    console.log(req.body.username);
    console.log(req.body.password);

    var newUser = new User({
        username:req.body.username,
        password:req.body.password,
        createtime:Date.now(),
        updatetime:Date.now()

    });


    User.findOne({username:newUser.username},function (err,user) {
        req.session.user = null;
        if(user){
            var dataError = {
                status: '0',
                msg: '用户名已存在'
            };
            res.end(JSON.stringify(dataError));
        }
        else if (err){
            var dataError = {
                status: '0',
                msg: err.message
            };
            res.end(JSON.stringify(dataError));
        }
        else {

            newUser.save(function(err){
                if(err){
                    var dataError = {
                        status: '0',
                        msg: err.message
                    };
                    res.end(JSON.stringify(dataError));
                }
                else {
                    req.session.user = user;
                    res.end(JSON.stringify(dataSuccess));
                }
             });




        }

    })



});


module.exports = router;
